from apscheduler.schedulers.blocking import BlockingScheduler
import subprocess
import json

def run_script(script_path):
    try:
        subprocess.run(['python', script_path], check=True)
        print(f"成功运行{script_path}.")
    except subprocess.CalledProcessError as e:
        print(f"运行出错{script_path}: {e}")

def schedule_script(script_path):

    scheduler = BlockingScheduler()

    def job():
        run_script(script_path)

        visited_articles_ID = json.load(open('visited_articles_ID.json', 'r', encoding="utf-8"))
        if len(visited_articles_ID) < 2000:#爬取数量
            scheduler.add_job(job)


    scheduler.add_job(job)
    scheduler.start()


if __name__ == "__main__":
    script_path = 'spid_twitter.py'#文件路径
    schedule_script(script_path)
